import IntegrationTable, {
	IntegrationTableProps,
} from "@/components/IntegrationTable";
import type { ProColumns, ActionType } from "@ant-design/pro-table";
import React, { useEffect, useState, useRef } from "react";
import { Card, Tabs, Row, Statistic, Col, Descriptions } from "antd";
import { DynamicFormItem } from "@/components/DynamicForm";
import { Tag } from "antd";
// @page 商家
const Business: React.FC<any> = () => {
	// 组件是否已经卸载
	let isUnMounted = false;
	const actionRef = useRef<ActionType>();
	const taskTypeList = [
		{ label: "拍厨余垃圾", value: 10 },
		{ label: "拍其他垃圾", value: 20 },
	];

	const taskStatusList = [
		{ label: "默认", value: 10 },
		{ label: "使用中", value: 20 },
	];
	// 表格项
	const columns: ProColumns[] = [
		{
			title: "用户ID",
			dataIndex: "id",
			search: false,
		},
		{
			title: "任务名称",
			dataIndex: "taskTitle",
			search: false,
		},
		{
			title: "任务说明",
			dataIndex: "taskDesc",
			search: false,
		},
		{
			title: "任务类型",
			dataIndex: "taskType",
			search: false,
			render: (_, record: any) => {
				return (
					<div>
						<Tag color="processing">
							{record.taskType == "10" ? "拍厨余垃圾" : "拍其他垃圾"}
						</Tag>
					</div>
				);
			},
		},
	];

	const getFormItems = (vm: any, isEdit?: boolean): DynamicFormItem[] => {
		const { coupon } = vm.state;
		return [
			{
				type: "text",
				label: "任务编号",
				name: "taskCode",
				required: true,
			},
			{
				type: "text",
				label: "任务标题",
				name: "taskTitle",
				required: true,
			},
			{
				type: "select",
				label: "任务类型",
				name: "taskType",
				options: taskTypeList,
				required: true,
			},
			{
				type: "text",
				label: "任务奖励",
				name: "rewardCode",
				required: true,
			},
			{
				type: "text",
				label: "奖励数量",
				name: "rewardNum",
			},
			{
				type: "text",
				label: "任务描述",
				name: "taskDesc",
				required: true,
			},
			{
				type: "upload",
				label: "任务Icon",
				name: "taskIcon",
				fieldProps: {
					limit: 1,
				},
			},
			{
				type: "select",
				label: "任务状态",
				name: "taskStatus",
				options: taskStatusList,
			},
		];
	};

	// 一体化表格props
	const tablePorps: IntegrationTableProps = {
		rowKey: "id",
		scroll: { x: 1000 },
		columns,
		search: false,
		actionRef,
		listProps: {
			url: "/task/page-list",
			method: "GET",
			requestType: "params",
			paramsHandle: (params: any) => {
				return {
					...params,
				};
			},
		},
		addProps: {
			url: "/task/save-or-update",
			method: "POST",
			requestType: "params",
			paramsHandle: (params: any) => {
				params.taskIcon = (
					params?.taskIcon?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
				};
			},
			modalProps: {
				formProps: {
					labelCol: { span: 6 },
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm);
				},
			},
		},
		updateProps: {
			url: "/task/save-or-update",
			method: "POST",
			requestType: "params",
			paramsHandle: ({ ...params }: any, record: any) => {
				params.taskIcon = (
					params?.taskIcon?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
				};
			},
			modalProps: {
				formValuesHandle: async (record: any) => {
					const { ...others } = record;
					return {
						...others,
						taskIcon: others?.taskIcon
							? [
									{
										uid: "-1",
										status: "done",
										name: others.taskIcon,
										url: others.taskIcon,
									},
							  ]
							: [],
					};
				},

				formProps: {
					labelCol: { span: 8 },
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm, true);
				},
			},
		},
		handleOperating: (items: any[]) => [
			{
				type: "pop",
				props: {
					url: "/task/delete",
					method: "POST",
					popProps: {
						title: "确定要删除此任务吗?",
					},
					btnText: "删除",
					paramsHandle: (params: any, record: any) => {
						return { ...params };
					},
				},
			},
			...items,
		],
	};

	// 初始化加载数据
	useEffect(() => {
		return () => {
			isUnMounted = true;
		};
	}, []);

	return <IntegrationTable {...tablePorps} />;
};

export default Business;
